<?php

/**
 * @Author: Wang Chunsheng 2192138785@qq.com
 * @Date:   2020-03-04 01:14:12
 * @Last Modified by:   Wang Chunsheng 2192138785@qq.com
 * @Last Modified time: 2020-03-16 04:21:52
 */


namespace api\modules\diandi_shop\controllers;

use Yii;
use api\controllers\AController;
use api\modules\diandi_shop\services\OrderService;
use common\helpers\ResultHelper;

class OrderController extends AController
{
  public $modelClass = '\common\models\DdGoods';

  public function actionSearch()
  {
    return [
      'error_code'    => 20,
      'res_msg'       => 'ok',
    ];
  }

  /**
   * @SWG\Post(path="/diandi_shop/order/createorder",
   *     tags={"订单"},
   *     summary="立即购买.",
   *     @SWG\Response(
   *         response = 200,
   *         description = "首页",
   *         @SWG\Schema(ref = "#/definitions/Docs")
   *     ),
   *     @SWG\Parameter(
   *      name="access-token",
   *      type="string",
   *      in="query",
   *      required=true
   *     ),
   *     @SWG\Parameter(
     *          in="formData",
     *          name="goods",
     *          type="string",
     *          description="商品数据",
     *          required=true,
     *   ),
   *     @SWG\Parameter(
     *          in="formData",
     *          name="total_price",
     *          type="integer",
     *          description="订单总额",
     *          required=true,
     *   ),
   *     @SWG\Parameter(
     *          in="formData",
     *          name="express_price",
     *          type="integer",
     *          description="运费",
     *          required=true,
     *   ),
   *     @SWG\Parameter(
     *          in="formData",
     *          name="express_type",
     *          type="integer",
   *          enum={0,1},
     *          description="收货方式0配送点配送1收货地址收货",
     *          required=true,
     *   ),
   *     @SWG\Parameter(
     *          in="formData",
     *          name="address_id",
     *          type="integer",
     *          description="配送区域id",
     *          required=true,
     *   ),

   *     @SWG\Parameter(
     *          in="formData",
     *          name="name",
     *          type="string",
     *          description="收货人",
     *          required=false,
     *   ),
   *   @SWG\Parameter(
     *          in="formData",
     *          name="phone",
     *          type="integer",
     *          description="手机号",
     *          required=true,
     *   ),
   *     @SWG\Parameter(
     *          in="formData",
     *          name="detail",
     *          type="string",
     *          description="详细的收货地址",
     *          required=true,
     *   ),
   * )
   */
  public function actionCreateorder()
  {


    $data = Yii::$app->request->post();
    $total_price    = $data['total_price'];
    $express_price = $data['express_price'];
    $express_type = $data['express_type'];

    $name = '';
    $phone = 0;
    $detail = '';


    if ($data['express_type'] == 0) {
      if (empty($data['address_id'])) {
        return ResultHelper::json(401, '请选择配送区域', []);
      }
      if (empty($data['name'])) {
        return ResultHelper::json(401, '请输入收货人姓名', []);
      }
      if (empty($data['phone'])) {
        return ResultHelper::json(401, '请输入收货人手机号', []);
      }
      if (empty($data['detail'])) {
        return ResultHelper::json(401, '请输入收货详细地址具体到楼层房间号', []);
      }

      $name = $data['name'];
      $phone = $data['phone'];
      $detail = $data['detail'];
    } else {
      if (empty($data['address_id'])) {
        return ResultHelper::json(401, '请选择收货地址', []);
      }
    }

    $address_id = $data['address_id'];

    $user_id = Yii::$app->user->identity->member_id;
    $goods = json_decode($data['goods'], true);
    $orderInfo =  OrderService::createOrder($user_id, $goods, $total_price, $express_price, $express_type, $address_id, $name, $phone, $detail);
    return ResultHelper::json(200, '创建订单成功', $orderInfo);
  }
  /**
   * @SWG\Post(path="/diandi_shop/order/cart",
   *     tags={"订单"},
   *     summary="购物车购买.",
   *     @SWG\Response(
   *         response = 200,
   *         description = "首页",
   *         @SWG\Schema(ref = "#/definitions/Docs")
   *     ),
   *     @SWG\Parameter(
       *     in="formData",
       *     name="access-token",
       *     type="string",
       *     description="用户秘钥",
       *     required=true,
       *   ),
   * )
   */
  public function actionCart()
  {
  }
  
 /**
   * @SWG\Post(path="/diandi_shop/order/confirm",
   *     tags={"订单"},
   *     summary="订单操作",
   *     @SWG\Response(
   *         response = 200,
   *         description = "订单操作",
   *         @SWG\Schema(ref = "#/definitions/Docs")
   *     ),
   *     @SWG\Parameter(
   *        in="formData",
   *        name="order_id",
   *        type="integer",
   *        description="订单id",
   *        required=true,
   *   ),
   *     @SWG\Parameter(
   *        in="formData",
   *        name="ctype",
   *        type="string",
   *        description="操作类型",
   *        enum={"qxdd","qrfh","qrsh","scdd"},
   *        required=true,
   *   ),
   *     @SWG\Parameter(
   *        in="query",
   *        name="access-token",
   *        type="string",
   *        description="用户秘钥",
   *        required=true,
   *   ),
   * )
   */
  public function actionConfirm()
  {
    $order_id = Yii::$app->request->post('order_id');
    $ctype    = Yii::$app->request->post('ctype');
    return OrderService::confirmOrder($order_id,$ctype);
  }

  
  /**
   * @SWG\Post(path="/diandi_shop/order/list",
   *     tags={"订单"},
   *     summary="订单列表.",
   *     @SWG\Response(
   *         response = 200,
   *         description = "首页",
   *         @SWG\Schema(ref = "#/definitions/Docs")
   *     ),
   *     @SWG\Parameter(
   *        in="query",
   *        name="access-token",
   *        type="string",
   *        description="用户秘钥",
   *        required=true,
   *   ),
   *     @SWG\Parameter(
   *        in="formData",
   *        name="order_status",
   *        type="integer",
   *        description="订单状态,全部不传递参数，具体的传递参数",
   *        required=false,
   *   ),
   *     @SWG\Parameter(
   *        in="query",
   *        name="page",
   *        type="integer",
   *        description="页码",
   *        required=true,
   *   ),
   *   @SWG\Parameter(
   *        in="formData",
   *        name="pageSize",
   *        type="integer",
   *        description="显示数量",
   *        required=true,
   *   ),
    
   * )
   */
  public function actionList()
  {
    $user_id = Yii::$app->user->identity->member_id;
    $pageSize = Yii::$app->request->post('pageSize');
    $order_status= Yii::$app->request->post('order_status','');
    $list = OrderService::list($user_id,$order_status, $pageSize);
    return ResultHelper::json(200, '获取成功', $list);
  }
  
  /**
   * @SWG\Post(path="/diandi_shop/order/detail",
   *     tags={"订单"},
   *     summary="订单详情.",
   *     @SWG\Response(
   *         response = 200,
   *         description = "订单详情",
   *         @SWG\Schema(ref = "#/definitions/Docs")
   *     ),
   *     @SWG\Parameter(
   *        in="query",
   *        name="access-token",
   *        type="string",
   *        description="用户秘钥",
   *        required=true,
   *   ),
   *     @SWG\Parameter(
   *        in="formData",
   *        name="order_id",
   *        type="integer",
   *        description="订单ID",
   *        required=true,
   *   ),
   * )
   */
  public function actionDetail()
  {
    $order_id = Yii::$app->request->post('order_id');
    $user_id = Yii::$app->user->identity->member_id;
    $res = OrderService::detail($order_id);
    return ResultHelper::json(200,'获取成功',$res);
  }

  /**
   * @SWG\Post(path="/diandi_shop/order/delivery",
   *     tags={"订单"},
   *     summary="确认收货.",
   *     @SWG\Response(
   *         response = 200,
   *         description = "首页",
   *         @SWG\Schema(ref = "#/definitions/Docs")
   *     ),
   *     @SWG\Parameter(
       *     in="formData",
       *     name="access-token",
       *     type="string",
       *     description="用户秘钥",
       *     required=true,
       *   ),
   * )
   */
  public function actionDelivery()
  {
  }

  /**
   * @SWG\Post(path="/diandi_shop/order/logistics",
   *     tags={"订单"},
   *     summary="物流跟踪.",
   *     @SWG\Response(
   *         response = 200,
   *         description = "首页",
   *         @SWG\Schema(ref = "#/definitions/Docs")
   *     ),
   *     @SWG\Parameter(
       *     in="formData",
       *     name="access-token",
       *     type="string",
       *     description="用户秘钥",
       *     required=true,
       *   ),
   * )
   */
  public function actionLogistics()
  {
  }


  /**
   * @SWG\Post(path="/diandi_shop/order/comment",
   *     tags={"订单"},
   *     summary="评价商品.",
   *     @SWG\Response(
   *         response = 200,
   *         description = "首页",
   *         @SWG\Schema(ref = "#/definitions/Docs")
   *     ),
   *     @SWG\Parameter(
       *     in="formData",
       *     name="access-token",
       *     type="string",
       *     description="用户秘钥",
       *     required=true,
       *   ),
   * )
   */
  public function actionComment()
  {
  }
}
